package com.xp.ai.aiservice;

import com.xp.ai.util.ModelUtils;
import dev.langchain4j.agent.tool.P;
import dev.langchain4j.agent.tool.Tool;
import dev.langchain4j.agent.tool.ToolSpecification;
import dev.langchain4j.agent.tool.ToolSpecifications;
import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.service.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Locale;


/***
 * 智能体模式
 * 高阶用法，可以不用去管底层实现细节
 * 只需要把需要的组件构建出来就可以了
 */
public class MyAiWriterService {


    interface Assist {
        Result<String> chat(@MemoryId Long memoryId, @UserMessage("userMessage") String content);
    }

    static class Tools {
        @Tool("当前日期")
        public String now() {
            return LocalDateTime.now().toString();
        }

        @Tool("获取某个城市的天气情况")
        public String getWeather(@P("指定城市") String location) {
            return location + "天气晴转多云";
        }
    }
    public static void main(String[] args) throws NoSuchMethodException {

        ChatLanguageModel chatLanguageModel = ModelUtils.getHuoshanv3Model();
        ChatMemory  chatMemory = MessageWindowChatMemory.withMaxMessages(10);
        List<ToolSpecification> toolSpecifications = ToolSpecifications.toolSpecificationsFrom(Tools.class);
        ToolSpecification toolSpecification = ToolSpecifications.toolSpecificationFrom(Tools.class.getMethod("now"));
        Tools tools = new Tools();
        Assist assist = AiServices.builder(Assist.class)
                .chatLanguageModel(chatLanguageModel)
                //这里是使用的 chatMemoryProvider 模式
                .chatMemoryProvider(memoryId -> MessageWindowChatMemory.withMaxMessages(10))
//                .tools(toolSpecifications)
                //调用本地方法要这么传才行
                .tools(tools)
                .build();

        Result<String> r1 = assist.chat(1L, "你好我是黄忠泽，今天是什么时间，天气怎样？");
        System.out.println(r1.content());
        Result<String> r11 = assist.chat(1L, "针对我的昵称，请做一首藏头诗");
        System.out.println(r11.content());

        Result<String> r2 = assist.chat(2L, "你好我是铃木爱理,今天是多少号，天气怎样？");
        System.out.println(r2.content());
        Result<String> r22 = assist.chat(2L, "针对我的昵称，请做一首藏头诗");
        System.out.println(r22.content());

    }
}
